package chances.epg.data.taglib.page;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import chances.epg.commons.utils.RequestUtils;
import chances.epg.data.PageBean;


public class PageInfoHelper {
	private static PageInfoHelper instance;
	
	protected PageInfoHelper() {
		
	}
	
	public static PageInfoHelper getInstance() {
		if(instance == null){
			instance = new PageInfoHelper();
		}
		
		return instance;
	}
	
	public PageInfo createPageInfo(int pageIndex,int pageSize){
		PageInfo pageInfo = new PageInfo(pageSize);
		if(pageIndex > 0) {
			pageInfo = new PageInfo(pageIndex, pageSize);
		}
		return pageInfo;
	}
	
	public void createTurnPageUrl(HttpServletRequest request,PageInfo pageInfo,String pageIndexName,boolean cycle) {
		this.createPrevUri(request, pageIndexName, cycle, pageInfo);
		this.createNextUri(request, pageIndexName, cycle, pageInfo);
	}
	
	/**
	 * 设置上一页链接.
	 * 如cycle为false，则前一页链接可能为空.
	 * @param pageInfo
	 */
	private void createPrevUri(HttpServletRequest request,String pageIndexName,boolean cycle,PageInfo pageInfo){
		PageBean pageBean = pageInfo.getPageBean();
		
		int curPageIndex = 1;
        if(pageBean.getCurrentPage() > 1){
			curPageIndex = pageBean.getCurrentPage() - 1;
			pageInfo.setPrevUri(this.generatorPageURL(request,pageIndexName,curPageIndex));
		}else{
			if(cycle){
				curPageIndex = pageBean.getPageCount();
				pageInfo.setPrevUri(this.generatorPageURL(request,pageIndexName,curPageIndex));
			}
		}
	}
	
	/**
	 * 设置下一页链接.
	 * 如cycle为false，则前一页链接可能为空.
	 * @param pageInfo
	 */
	private void createNextUri(HttpServletRequest request,String pageIndexName,boolean cycle,PageInfo pageInfo) {
        PageBean pageBean = pageInfo.getPageBean();
		
		int curPageIndex = 1;
		
        if(pageBean.getCurrentPage() < pageBean.getPageCount()){
			curPageIndex = pageBean.getCurrentPage() + 1;
			pageInfo.setNextUri(this.generatorPageURL(request,pageIndexName,curPageIndex));
		}else{
			if(cycle){
				curPageIndex = 1;
				pageInfo.setNextUri(this.generatorPageURL(request,pageIndexName,curPageIndex));
			}
		}
	}
	
	/**
	 * 根据页索引生成翻页信息.
	 * @param curPageIndex
	 * @return
	 */
	private String generatorPageURL(HttpServletRequest request,String pageIndexName,int curPageIndex) {
		String uri = RequestUtils.getRequestPath(request);
		String queryString = null;
		
		Map<String,String[]> params = request.getParameterMap();
		//没参数
		if(params.size() <= 0) {
			queryString = pageIndexName + "=" + curPageIndex;
			return uri + "?" + queryString;
		}
		
		for (String key : params.keySet()) {
		    if("efid".equals(key)){
		    	continue;
		    }
		    
		    String value = request.getParameter(key);
		    if(pageIndexName.equals(key)){
		    	value = curPageIndex + "";
		    }
		    
		    if(StringUtils.isBlank(queryString)){
		    	queryString = key + "=" + value;
		    }else{
		    	queryString = queryString + "&" + key + "=" + value;
		    }
		}
		if(null == queryString || queryString.indexOf(pageIndexName) == -1){
			queryString = pageIndexName + "=" + curPageIndex;
		}
		return uri + "?" + queryString;
	}
}
